package com.hundsun.cprs.yyt.web.action.report;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hundsun.cprs.yyt.service.report.service.DaliyClinchSumService;
import com.hundsun.cprs.yyt.service.trade.domain.query.TradeMatchDetailQuery;
import com.hundsun.cprs.yyt.service.trade.dto.TradeMatchDetailInfo;
import com.hundsun.cprs.yyt.service.trade.enums.EnumEntustBSType;
import com.hundsun.cprs.yyt.service.trade.service.TradeMatchDetailService;
import com.hundsun.cprs.yyt.util.DateUtil;
import com.hundsun.cprs.yyt.util.ExportExcelUtil;
import com.hundsun.cprs.yyt.web.action.BaseAction;
import com.hundsun.cprs.yyt.web.util.ExportExcelWrapper;
import com.hundsun.cprs.yyt.web.vo.excel.TradeMatchDetailVo;

@Controller
@RequestMapping("report/")
public class MatchDetailReportAction extends BaseAction {

    @Autowired
    private TradeMatchDetailService tradeMatchDetailService;

    /**
     * 成交明细报表
     * @param query
     *
     * */
    @RequestMapping("clinch/detail.htm")
    public void clinchReportDetail(@ModelAttribute("query")TradeMatchDetailQuery query,ModelMap model){
        if (query.getStartDate() == null){
            query.setStartDate(DateUtil.getBeginOfDay(new Date()));
        }
        if (query.getEndDate() == null){
            query.setEndDate(DateUtil.getEndOfDay(new Date()));
        }
        tradeMatchDetailService.queryDetailByPage(query);
        model.addAttribute("EnumEntustBSTypeMap",EnumEntustBSType.toMap());
    }
    
    @RequestMapping("clinch/export/excel")
    public void exportExcel(TradeMatchDetailQuery query,HttpServletRequest request, HttpServletResponse response) throws Exception {
        query.setPageSize(30000);
        if (query.getStartDate() == null){
            query.setStartDate(DateUtil.getBeginOfDay(new Date()));
        }
        if (query.getEndDate() == null){
            query.setEndDate(DateUtil.getEndOfDay(new Date()));
        }
        tradeMatchDetailService.queryDetailByPage(query);
        
        List<TradeMatchDetailVo> exportResult = new ArrayList<TradeMatchDetailVo>();
        for(TradeMatchDetailInfo item : query.getData()){
            exportResult.add(new TradeMatchDetailVo(item));
        }
        
        String[] columnNames = { "流水号","交易账号","商品编号","商品名称","买卖方向","委托数量","委托金额","匹配数量","匹配金额","匹配时间"};
        String fileName = "成交数据导出";
        ExportExcelWrapper<TradeMatchDetailVo> util = new ExportExcelWrapper<TradeMatchDetailVo>();
        util.exportExcel(fileName, fileName, columnNames, exportResult, response, ExportExcelUtil.EXCEl_FILE_2007);
    }
    
    @Override
    protected void initBinder(WebDataBinder binder) {
        // 注册默认的日期格式化类型: yyyy-MM-dd
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }
}
